package com.atguigu.gmall.web.controller;

import com.atguigu.gmall.activity.client.ActivityFeignClient;
import com.atguigu.gmall.common.result.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @Author atguigu-mqx
 * @Description TODO
 * @Date 2023/9/9 9:30
 * @Version 1.0
 */
@Controller
public class SeckillController {

    @Autowired
    private ActivityFeignClient activityFeignClient;

    //  http://activity.gmall.com/seckill.html

    /**
     * 查看秒杀列表
     * @param model
     * @return
     */
    @GetMapping("seckill.html")
    public String seckill(Model model){
        //  查询数据
        model.addAttribute("list",activityFeignClient.findAll().getData());
        //  秒杀列表页面
        return "seckill/index";
    }

    //  /seckill/'+${item.skuId}+'.html'

    /**
     * 秒杀详情
     * @param skuId
     * @param model
     * @return
     */
    @GetMapping("/seckill/{skuId}.html")
    public String seckillItem(@PathVariable Long skuId, Model model){
        //  存储数据 item
        model.addAttribute("item",activityFeignClient.getSeckillGoods(skuId).getData());
        return "seckill/item";
    }

    /**
     * 来到排队页面
     * @param request
     * @return
     */
    @GetMapping("seckill/queue.html")
    public String seckillQueue(HttpServletRequest request){
        //  存储数据
        request.setAttribute("skuId",request.getParameter("skuId"));
        request.setAttribute("skuIdStr",request.getParameter("skuIdStr"));
        //  来到排队页面
        return "seckill/queue";
    }

    /**
     * 秒杀结算页
     * @return
     */
    @GetMapping("/seckill/trade.html")
    public String seckillTrade(Model model){
        //  userAddressList-异步ajax请求获取的  detailArrayList-orderDetail totalNum  totalAmount
        //  model.addAttribute("");
        Result<Map<String,Object>> result = this.activityFeignClient.trade();
        //  判断 code == 200
        if (result.isOk()){
            model.addAllAttributes(result.getData());
            //  秒杀结算页
            return "seckill/trade";
        } else {
            //  存储信息
            model.addAttribute("message","下单失败");
            return "seckill/fail";
        }

    }
}
